import { customTable } from '@/components/ProTable/interface';
import { ProTableValue } from '@/interface/public';

import { formOpen } from '@/utils/form';
import { useAddForm } from './use-addForm';
import { useChangePwdForm } from './use-changePwd';
import { UserInterface } from '@/api/interface/auth/user';
import { useBindingRoleForm } from './use-bindingRoleForm';
import { PublicParams } from '@/api/interface/publicParams';

export function useDataGrid(proTableBox: ProTableValue) {
  const { addForm, saveAddForm } = useAddForm(proTableBox);
  const { changePwdForm, savePwdForm } = useChangePwdForm(proTableBox);

  const { bindingRoleForm, saveAddBindingRoleForm, setBindingFormChildren } = useBindingRoleForm(proTableBox);
  let dataGrid = reactive<customTable>({
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'right', width: 240 },
      {
        prop: 'account',
        label: '账号',
        search: {
          el: 'input',
          key: 'key',
          label: 'keyValue',
          props: {
            placeholder: 'userPermissions_key'
          }
        }
      },
      {
        prop: 'name',
        label: '姓名'
      },
      {
        prop: 'email',
        label: '邮箱'
      },
      {
        prop: 'phone',
        label: '手机号'
      },
      {
        prop: 'gender',
        label: '性别',
        enum: genderStatus
      },
      {
        prop: 'status',
        label: '状态',
        tag: true,
        enum: enableOrDisabledStatus
      },
      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: 'addNewUsers',
        type: 'primary',
        icon: 'Plus',
        // permissionCode: 'p:auth:user:save',
        methods: async () => {
          formOpen<UserInterface.ReqSave>(addForm, 'addNewUsers');
        }
      }
    ],
    rowButtonList: [
      {
        label: 'edit',
        type: 'primary',
        // permissionCode: 'p:auth:user:save',
        methods: (row: UserInterface.ResUser) => {
          formOpen<UserInterface.ReqSave>(addForm, 'editUsers', row);
        },
        attrs: {
          link: true
        }
      },
      {
        label: '角色绑定',
        type: 'primary',
        // permissionCode: 'p:auth:user:save',
        methods: (row: UserInterface.ResUser) => {
          const params = {
            targetId: row.id
          };
          formOpen<PublicParams.ResBindingId>(bindingRoleForm, `角色绑定`, params);
          setBindingFormChildren({ id: row.id });
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'changePassword',
        type: 'primary',
        // permissionCode: 'p:auth:user:upPwd',
        methods: (row: UserInterface.ResUser) => {
          formOpen<UserInterface.ReqAdminChangeUserByPwd>(changePwdForm, 'changePassword', row);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'enable',
        type: 'primary',
        // permissionCode: 'p:auth:user:status',
        display: item => item.status !== BasicKey.ENABLE,
        methods: (row: UserInterface.ResUser) => {
          const params = { id: row.id, status: 1 };
          useHandleData(userUpdateStatus, params, `确定启用【${row.name}】账号`, proTableBox.value?.getTableList);
        },
        attrs: {
          link: true
        }
      },
      {
        label: 'disabled',
        type: 'danger',
        // permissionCode: 'p:auth:user:status',
        display: item => item.status !== BasicKey.DISABLE,
        methods: (row: UserInterface.ResUser) => {
          const params = { id: row.id, status: 0 };
          useHandleData(userUpdateStatus, params, `确定禁用【${row.name}】账号`, proTableBox.value?.getTableList);
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  return { dataGrid, changePwdForm, addForm, saveAddForm, savePwdForm, bindingRoleForm, saveAddBindingRoleForm };
}
